/**
 *  *2010-8-16 下午05:16:49  add 
 */
package com.base.myproject.client.HR.batchpro;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.HR.bcode.BcodeSelectDialog;
import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.busi.BO.TreeNodeModelData;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.celleditor.BaseComboBoxX;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.LabelField;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckNodes;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Administrator
 *2010-8-16
 * javazgw@gmail.com
 */
public class BcodeLeave extends ProcItem{
	
	boolean isinit = false;
	BcodeSelectDialog<TreeNodeModelData> bsd_add = null;
	
	//合同开始日期
	DateField Resignationdate;;
	//合同结束日期
	DateField contract_end_date;;
	//合同开始日期添加天数
	NumberField start_n; 
	//合同结束日期添加天数
	NumberField end_n; 
	Button save = new Button("保存");
	
	BaseComboBoxX C_num8 = new BaseComboBoxX("name","dictionary", "where type='zyzk'");
	BaseComboBoxX C_num9 = new BaseComboBoxX("name","dictionary", "where type='hr_bcodeleave'");
	LabelField leavereason_l = new LabelField("离职原因：");
	LabelField mark_l = new LabelField("离职备注：");
	TextArea mark_t = new TextArea(); 
	
	LabelField resignationdate_l = new LabelField("离职日期为：");
	LayoutContainer lc = new LayoutContainer();
	
	public BcodeLeave() {
		init();
	}

	public BcodeLeave(String name) {
		super(name);
	}

	private void init() {

	}

	@Override
	public Widget initwidget() {
		if(!isinit)
		{
		HorizontalPanel hpanel = new HorizontalPanel();
		createGrid();
		LayoutContainer lc = initProButtons();
		
		hpanel.add(grid);
		hpanel.add(lc);
		this.add(hpanel);
		isinit = true;
		Tools.setResize(grid, 0.8,0.95);
		Tools.setResize(lc, 0.2,0.95);
		}
		return this;
	}
	
	BaseStore<BusiObject> store = null;

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("employstatus");
	
		type.addField("bcode");
		type.addField("leavereason");
		type.addField("leavemark");
		type.addField("Resignationdate");
		type.getField("Resignationdate").setType(Date.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("bcode_job");
		info.setKeyname(new String[] { "icode" });
	//	info.setSql("select contractstartdate,contractsenddate from bcode_job");
		info.setNeedAllRow("true");
		info.setForcenolimit("true");
		// info.setParent(mainstore);// 关联主表
		// info.setLinkparentcolumns(new String[] { "icode" }); // 关联主表字段
		// info.setLinkparent_chile_columns(new String[] { "upicode" });//
		// 关联主表字段
		store = new BaseStore<BusiObject>(type, info,false);
		return store;
	}

	BaseGrid grid = null;

	BaseGrid createGrid() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

//		ColumnConfig C3 = new ColumnConfig("icode",
//				"icode", 180);
//		columns.add(C3);
		
		ColumnConfig C1 = new ColumnConfig("bcode",
				"人员", 100);

		columns.add(C1);
		ColumnConfig C2 = new ColumnConfig("employstatus",
				"状态", 100);
		TextField<String> C2_t = new TextField<String>();
		C2.setEditor(new CellEditor(C2_t));		
		columns.add(C2);
		
		ColumnConfig C3 = new ColumnConfig("leavereason",
				"离职原因", 100);
		TextField<String> C3_t = new TextField<String>();
		C3.setEditor(new CellEditor(C3_t));	
		C3.setHidden(true);
		columns.add(C3);
		
		
		ColumnConfig C4 = new ColumnConfig("leavemark",
				"离职备注", 100);
		TextField<String> C4_t = new TextField<String>();
		C4.setEditor(new CellEditor(C4_t));	
		C4.setHidden(true);
		columns.add(C4);
		
		
		
		ColumnConfig C5 = new ColumnConfig("Resignationdate",
				"离职日期", 180);
		C5.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
//		C5.setEditor(new CellEditor(C4_t));	
		C5.setHidden(true);
		columns.add(C5);

		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid(getStore(), cm);
		cbs.setSelectionMode(SelectionMode.SIMPLE);
		grid.setSelectionModel(cbs);
		return grid;
	}
	
	LayoutContainer initProButtons()
	{
	
		TableLayout tl = new TableLayout(2);
		lc.setLayout(tl);
		
		TableData td1 = new TableData();
		td1.setColspan(1);
		TableData td2 = new TableData();
		td2.setColspan(2);
		
		 bsd_add = new BcodeSelectDialog<TreeNodeModelData>(){
			public void doposted(List<TreeNodeModelData> modeldatalist) {
				if(modeldatalist.size()>0)
				{
					this.setEmptyText("已经选择了数据");
					
					List<TreeNodeModelData>  selectchecklist=bsd_add.getTree().getCheckedSelection();
					
					if(selectchecklist.size()<=0)
					{
						MessageBox.alert("错误", "请选择“离职人员”", null	);
						return;
					}
					String bcodestr = "";
					for(TreeNodeModelData tnmd:selectchecklist)
					{
						BusiObject bo = new BusiObject();
						
						bcodestr +=" systembcode like '"+tnmd.get("systembcode")+"%25' or ";

					}
					bcodestr +=" 1=2";
					GWT.log("bcodestr ="+bcodestr);
					
					
					String sql  = "select icode, bcode,employstatus,leavereason,leavemark,Resignationdate from bcode_job where exists (select * from systembcode_view where systembcode_view.bcode = bcode_job.bcode  and ( "+bcodestr+"))";
					GWT.log("sql ="+sql);
					store.getStoreinfo().setSql(sql);
					store.reload();
				}
			}

		};
		bsd_add.setNeesreselect(false);
		bsd_add.setEmptyText("未选择数据");
		bsd_add.setExpanded();
		bsd_add.setCheckNodes(CheckNodes.BOTH);
		
		
		Resignationdate = new DateField();
		 Resignationdate.getPropertyEditor().setFormat(
					DateTimeFormat.getFormat("yyyy-MM-dd"));
		
		save.addListener(Events.OnClick,  getListener());
		lc.add(new LabelField("选择人员："),td1);
		lc.add(bsd_add,td1);
		
		lc.add(new LabelField("状态改变为："),td1);
		lc.add(C_num8,td1);
		
		lc.add(leavereason_l,td1);
		lc.add(C_num9,td1);
		
		lc.add(resignationdate_l,td1);
		lc.add(Resignationdate,td1);
		
		lc.add(mark_l,td1);
		lc.add(mark_t,td1);
		
		leavereason_l.setVisible(false);
		C_num9.setVisible(false);
		mark_l.setVisible(false);
		mark_t.setVisible(false);
		resignationdate_l.setVisible(false);
		Resignationdate.setVisible(false);
		
		lc.add(save,td2);
		
		bsd_add.addListener(Events.Changed, getListener());
		C_num8.addListener(Events.Changed, getListener());
		C_num9.addListener(Events.Changed, getListener());
		mark_t.addListener(Events.Changed, getListener());
		Resignationdate.addListener(Events.Changed, getListener());
		return lc;
	}
	
	int sync = 0;                                                      
	Listener<BaseEvent> defaultlistener;                               
                                                                     
	Listener<BaseEvent> getListener() {                                
		if(defaultlistener!=null)                                        
		{                                                                
			return defaultlistener;                                        
		}                                                                
                                                                     
		defaultlistener = new Listener<BaseEvent>() {                    
			public void handleEvent(BaseEvent be) {                        
				sync++;                                                      
				try {                                                        
					if (sync == 1) {                                           
						if (be.getType() == Events.OnClick) {                     
							if (be.getSource().equals(save)) {     
								store.trySave();
							}                                                      
						} else if (be.getType() == Events.Changed) {              
							if (be.getSource().equals(bsd_add)) {
								
								
							
							}     
							
							else if (be.getSource().equals(C_num8)) {
								if(C_num8.getValue().get("value").equals("离职"))
								{
									leavereason_l.setVisible(true);
									C_num9.setVisible(true);
									mark_l.setVisible(true);
									mark_t.setVisible(true);
									
									resignationdate_l.setVisible(true);
									Resignationdate.setVisible(true);
									grid.getColumnModel().setHidden(4,false);
									grid.getColumnModel().setHidden(5,false);
									grid.getColumnModel().setHidden(6,false);

								}
								else
								{
									leavereason_l.setVisible(false);
									C_num9.setVisible(false);
									mark_l.setVisible(false);
									mark_t.setVisible(false);
									
									resignationdate_l.setVisible(false);
									Resignationdate.setVisible(false);
									grid.getColumnModel().setHidden(4,true);
									grid.getColumnModel().setHidden(5,true);
									grid.getColumnModel().setHidden(6,true);

								}
								
								
								int i = 0;
								for(BusiObject bo:getStore().getModels())
								{
								
										grid.setValue(i,"employstatus",C_num8.getValue().get("value") );
									i++;
								}
								
							}
							

							else if (be.getSource().equals(C_num9)) {
								int i = 0;
								for(BusiObject bo:getStore().getModels())
								{
								
										grid.setValue(i,"leavereason",C_num9.getValue().get("value") );
									i++;
								}
							} 
							
							else if (be.getSource().equals(mark_t)) {
								int i = 0;
								for(BusiObject bo:getStore().getModels())
								{
								
										grid.setValue(i,"leavemark",mark_t.getValue() );
									i++;
								}
							} 
							else if (be.getSource().equals(Resignationdate)) {
								int i = 0;
								for(BusiObject bo:getStore().getModels())
								{
								
										grid.setValue(i,"Resignationdate",Resignationdate.getValue() );
									i++;
								}
							} 
							
							
						} else if (be.getType() == Events.KeyUp) {               
							if (be instanceof ComponentEvent) {                    
								if (((ComponentEvent) be).getKeyCode() == 13) // 回车
								{                                                    
								}                                                    
							}                                                      
						}                                                        
					}                                                          
				} finally {                                                  
					sync--;                                                    
				}                                                            
			}                                                              
                                                                     
		};                                                               
		return defaultlistener;                                          
	}     
}
